#!/bin/bash
if [ -e /etc/zabbix/scripts/tcpmon/setenv.sh ]; then
    source /etc/zabbix/scripts/tcpmon/setenv.sh
fi

IFS=$'\n'
SS_ALL=$(ss -tna | tail -n +2 | tr -s " " | cut -d " " -f1,4)

function TCP_STATS_COUNT() {
    if [ -z "$2" ]; then
        echo "$SS_ALL" | grep $1 | wc -l
    else
        echo "$SS_ALL" | egrep $2$ | grep $1 | wc -l
    fi
}

if [ -n "$PORT_LIST" ]; then
    SS_P_ESTAB=$(TCP_STATS_COUNT ESTAB)
    SS_P_SYN_SENT=$(TCP_STATS_COUNT SYN-SENT)
    SS_P_SYN_RECV=$(TCP_STATS_COUNT SYN-RECV)
    SS_P_FIN_WAIT_1=$(TCP_STATS_COUNT FIN-WAIT-1)
    SS_P_FIN_WAIT_2=$(TCP_STATS_COUNT FIN-WAIT-2)
    SS_P_TIME_WAIT=$(TCP_STATS_COUNT TIME-WAIT)
    SS_P_CLOSED=$(TCP_STATS_COUNT CLOSED)
    SS_P_CLOSE_WAIT=$(TCP_STATS_COUNT CLOSE-WAIT)
    SS_P_LAST_ACK=$(TCP_STATS_COUNT LAST-ACK)
    SS_P_LISTEN=$(TCP_STATS_COUNT LISTEN)
    SS_P_CLOSING=$(TCP_STATS_COUNT CLOSING)
    echo -n '{"LocalPort":{"all":[{"ESTABLISHED":'"$SS_P_ESTAB"',"SYNSENT":'"$SS_P_SYN_SENT"',"SYNRECV":'"$SS_P_SYN_RECV"',"FINWAIT1":'"$SS_P_FIN_WAIT_1"',"FINWAIT2":'"$SS_P_FIN_WAIT_2"',"TIMEWAIT":'"$SS_P_TIME_WAIT"',"CLOSED":'"$SS_P_CLOSED"',"CLOSEWAIT":'"$SS_P_CLOSE_WAIT"',"LASTACK":'"$SS_P_LAST_ACK"',"LISTEN":'"$SS_P_LISTEN"',"CLOSING":'"$SS_P_CLOSING"'}],'
    for s in $PORT_LIST; do
        PORT=$(echo $s | cut -d" " -f1)
        SS_P_ESTAB=$(TCP_STATS_COUNT ESTAB "$PORT")
        SS_P_SYN_SENT=$(TCP_STATS_COUNT SYN-SENT "$PORT")
        SS_P_SYN_RECV=$(TCP_STATS_COUNT SYN-RECV "$PORT")
        SS_P_FIN_WAIT_1=$(TCP_STATS_COUNT FIN-WAIT-1 "$PORT")
        SS_P_FIN_WAIT_2=$(TCP_STATS_COUNT FIN-WAIT-2 "$PORT")
        SS_P_TIME_WAIT=$(TCP_STATS_COUNT TIME-WAIT "$PORT")
        SS_P_CLOSED=$(TCP_STATS_COUNT CLOSED "$PORT")
        SS_P_CLOSE_WAIT=$(TCP_STATS_COUNT CLOSE-WAIT "$PORT")
        SS_P_LAST_ACK=$(TCP_STATS_COUNT LAST-ACK "$PORT")
        SS_P_LISTEN=$(TCP_STATS_COUNT LISTEN "$PORT")
        SS_P_CLOSING=$(TCP_STATS_COUNT CLOSING "$PORT")
        echo -n '"'"$PORT"'":[{"ESTABLISHED":'"$SS_P_ESTAB"',"SYNSENT":'"$SS_P_SYN_SENT"',"SYNRECV":'"$SS_P_SYN_RECV"',"FINWAIT1":'"$SS_P_FIN_WAIT_1"',"FINWAIT2":'"$SS_P_FIN_WAIT_2"',"TIMEWAIT":'"$SS_P_TIME_WAIT"',"CLOSED":'"$SS_P_CLOSED"',"CLOSEWAIT":'"$SS_P_CLOSE_WAIT"',"LASTACK":'"$SS_P_LAST_ACK"',"LISTEN":'"$SS_P_LISTEN"',"CLOSING":'"$SS_P_CLOSING"'}],'
    done | sed -e 's:\],$:\]:'
    echo -n '}}'
else
    SS_P_ESTAB=$(TCP_STATS_COUNT ESTAB)
    SS_P_SYN_SENT=$(TCP_STATS_COUNT SYN-SENT)
    SS_P_SYN_RECV=$(TCP_STATS_COUNT SYN-RECV)
    SS_P_FIN_WAIT_1=$(TCP_STATS_COUNT FIN-WAIT-1)
    SS_P_FIN_WAIT_2=$(TCP_STATS_COUNT FIN-WAIT-2)
    SS_P_TIME_WAIT=$(TCP_STATS_COUNT TIME-WAIT)
    SS_P_CLOSED=$(TCP_STATS_COUNT CLOSED)
    SS_P_CLOSE_WAIT=$(TCP_STATS_COUNT CLOSE-WAIT)
    SS_P_LAST_ACK=$(TCP_STATS_COUNT LAST-ACK)
    SS_P_LISTEN=$(TCP_STATS_COUNT LISTEN)
    SS_P_CLOSING=$(TCP_STATS_COUNT CLOSING)
    echo -n '{"LocalPort":{"all":[{"ESTABLISHED":'"$SS_P_ESTAB"',"SYNSENT":'"$SS_P_SYN_SENT"',"SYNRECV":'"$SS_P_SYN_RECV"',"FINWAIT1":'"$SS_P_FIN_WAIT_1"',"FINWAIT2":'"$SS_P_FIN_WAIT_2"',"TIMEWAIT":'"$SS_P_TIME_WAIT"',"CLOSED":'"$SS_P_CLOSED"',"CLOSEWAIT":'"$SS_P_CLOSE_WAIT"',"LASTACK":'"$SS_P_LAST_ACK"',"LISTEN":'"$SS_P_LISTEN"',"CLOSING":'"$SS_P_CLOSING"'}]}}'
fi

unset IFS
